package jgl.vajra.kingkong.dialogFragment;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;

import androidx.fragment.app.DialogFragment;

import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.MarketPriceBean;
import jgl.vajra.kingkong.bean.VersionUpdateBean;
import jgl.vajra.kingkong.databinding.DialogFragmentOrderDelayBinding;
import jgl.vajra.kingkong.databinding.DialogFragmentSignInBinding;
import jgl.vajra.kingkong.event.PaymentSuccessEvent;
import jgl.vajra.kingkong.listener.OrderDelayListener;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.manager.FragmentManager;
import jgl.vajra.kingkong.part.vehicle.activity.OrderStatusActivity;
import jgl.vajra.kingkong.widget.dialogfragment.BaseDialogFragment;
import jgl.vajra.kingkong.widget.manager.RxJavaHttpManager;
import jgl.vajra.kingkong.widget.manager.retrofit.RetrofitJsonManager;
import jgl.vajra.kingkong.widget.retrofithelper.rxsubscriber.ProgressObserver;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.kingkong.widget.utils.AppUtils;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

public class OrderDelayDialogFragment extends BaseDialogFragment<DialogFragmentOrderDelayBinding> {

    private OrderDelayedTimeDialogFragment orderDelayedTimeDialogFragment;
    private OrderStatusActivity mActivity;
    private String orderId;
    private int time;
    private int priceType = 0;
    private MarketPriceBean marketPriceBean;
    private String price;
    private OrderDelayListener listener;

    @Override
    public void initView(Context context, View rootView, AttributeSet attrs) {
        mBinding.setView(this);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetEdit);
    }

    @Override
    public void initData(Context context) {
    }

    @Override
    public void initEvent(Context context) {

    }


    public int getLayoutRes() {
        return R.layout.dialog_fragment_order_delay;
    }


    @Override
    public int getGravity() {
        return Gravity.CENTER;
    }

    public void setData(OrderStatusActivity mActivity, String orderId) {
        this.mActivity = mActivity;
        this.orderId = orderId;
    }

    //延时时间
    public void onDelayedTime() {
        if (orderDelayedTimeDialogFragment == null)
            orderDelayedTimeDialogFragment = new OrderDelayedTimeDialogFragment();
        orderDelayedTimeDialogFragment.setOrderDelayedTimeListener((position, name) -> {
            mBinding.tvDelayTime.setText(name);
            mBinding.ivMarketPrice.setSelected(true);
            mBinding.ivMyPrice.setSelected(false);
            priceType = 0;

            time = position + 1;
            Map<String, Object> map = new HashMap<>();
            map.put("authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
            map.put("orderId", AEACCoder.encrypt(orderId + "&&" + time));
            RetrofitJsonManager.getInstance().getApiService().getOrderDelayPriceData(map)
                    .compose(RxJavaHttpManager.applyTransformer())
                    .subscribe(new ProgressObserver<MarketPriceBean>(false, null) {
                        @Override
                        public void _onNext(MarketPriceBean data) {
                            marketPriceBean = data;
                            mBinding.tvMarketPrice.setText(data.price);
                        }

                        @Override
                        public void _onError(String errorMessage, int errorCode) {
                            super._onError(errorMessage, errorCode);

                        }
                    });

        });
        orderDelayedTimeDialogFragment.show(mActivity.getSupportFragmentManager());
    }

    //系统价格
    public void onMarketPrice() {
        priceType = 0;
        mBinding.ivMarketPrice.setSelected(true);
        mBinding.ivMyPrice.setSelected(false);
    }

    //我的出价
    public void onMyPrice() {
        priceType = 1;
        mBinding.ivMarketPrice.setSelected(false);
        mBinding.ivMyPrice.setSelected(true);
    }

    //确定
    public void onPositive() {
        if (priceType == 1) {
            String num = mBinding.etMyPrice.getText().toString().trim();
            if (TextUtils.isEmpty(num)) {
                ToastUtils.showShort("我的出价不能为空");
                return;
            }

            if (Double.parseDouble(marketPriceBean.lowestPrice) > Double.parseDouble(num)) {
                ToastUtils.showShort("我的出价不能低于市场价格" + marketPriceBean.lowestPrice);
                return;
            }
            if (Double.parseDouble(marketPriceBean.highestPrice) < Double.parseDouble(num)) {
                ToastUtils.showShort("我的出价不能高于市场价格" + marketPriceBean.highestPrice);
                return;
            }
            price = num;
        } else {
            if (marketPriceBean == null || TextUtils.isEmpty(marketPriceBean.price)) {
                ToastUtils.showShort("请选择延时时长");
                return;
            }
            price = marketPriceBean.price;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("orderId", AEACCoder.encrypt(orderId + "&&" + price));
        map.put("hour", time);
        RetrofitJsonManager.getInstance().getApiService().getOrderDelayApplyData(map)
                .compose(RxJavaHttpManager.applyTransformer())
                .subscribe(new ProgressObserver<String>(false, null) {
                    @Override
                    public void _onNext(String data) {
                        ToastUtils.showShort(data);
                        listener.onDetermine();
                        dismiss();
                    }

                    @Override
                    public void _onError(String errorMessage, int errorCode) {
                        super._onError(errorMessage, errorCode);

                    }
                });
    }

    public void setOrderDelayListener(OrderDelayListener listener){
        this.listener =listener;
    }
}